<?php
/**
**名称:天猫折扣精品库的规则管理
**作用:天猫折扣精品库的规则管理
**作者:张剑 swordphp@126.com
**创建日期:2013-07-06
**最后修改日期:2013-07-29
**/
require PHPCMS_PATH.'api/taobao_api/TopSdk.php';
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
class tmall_items extends admin{
	public function __construct(){
		parent::__construct();
		$this ->jihe_db = pc_base::load_model('jihe_model');
		$this ->item_db = pc_base::load_model('tmall_zhekou_model');
		$this ->tree = pc_base::load_sys_class('tree');
		$this ->taobao_api = new TopClient;//淘宝数据接口类
		$this ->taobao_api ->appkey = '';//申请的淘宝appkey
		$this ->taobao_api ->secretKey = '';//同上
		$this ->taobao_api ->format = 'json';//设定淘宝返回数据为json
	}
	public function init(){
		if($_GET['submit'] == 'submit'){
			$ob_sort = $this->__tmall_sort_by();
			$para['q'] = $_GET['q'];
			$para['brand'] = $_GET['brand'];
			$para['sort'] = $_GET['sort'];
			$para['post_fee'] = $_GET['post_fee'];
			$para['start_price'] = isset($_GET['start_price'])?$_GET['start_price']:'';
			$para['end_price'] = isset($_GET['end_price'])?$_GET['end_price']:'';
			$para['start'] = $_GET['start'];
			$para['cat'] = $_GET['cat'];
			$infos = $this->get_tmall_zhekou_items($para);
			//var_dump($infos);
			include $this->admin_tpl('tmall_zhekou');
		}else{
			$ob_sort = $this->__tmall_sort_by();
			include $this->admin_tpl('tmall_zhekou');
		}
	}
	public function add_rules(){
		echo serialize($_POST);
	}
	public function re_data(){
		$jid = isset($_GET['j_id'])?$_GET['j_id']:'';
		if($jid != ''){
			$j_info = $this->jihe_db->get_one("id = $jid");
			$paras = explode("|",$j_info['attr']);
			if(is_array($paras)){
				$count = 0;
				foreach($paras as $val){
					$rule = unserialize($val);
					if(is_array($rule)){
						$data = $this->get_tmall_zhekou_items($rule);
						if(is_array($data->item_list->tmall_search_item)){
							$data = $data->item_list->tmall_search_item;
							foreach($data as $val){
								$count++;
								$info['tb_item_id'] = $val->item_id;
								$info['item_title'] = $val->title;
								$info['detail_url'] = $val->url;
								$info['tbk_url'] = $val->url;
								$info['sole'] = $val->sold;
								$info['pic_path'] = $val->pic_path;
								$info['price'] = $val->price;
								$info['pro_price'] = $val->price_with_rate;
								$info['tb_nick'] = $val->nick;
								$info['item_promo_name'] = '';
								$info['shop_promo_name'] = '';
								$info['add_time'] = time();
								$info['order_id'] = $val->sold;
								//$info['shop_promo_detail'] = $val['shop_promo_detail_desc'];
								$info['j_id'] = $jid;
								$this->jihe_item->delete(array(tb_item_id => $info['tb_item_id']));
								$info = $this->__get_m_url($info);
								$this->jihe_item->insert($info);
							}
						}else{
							echo "在规则".print_r($rule,1)."上返回的数据无效!"."</br>";
						}
					}else{
						echo "规则格式错误!";
					}
				}
				echo "更新完成,更新了".$count."条数据"."</br>";
			}else{
				showmessage(L('集合数据规则为空!'),"?m=tmall_zhekou&c=sgw_jizi&a=init");
			}
			return false;
		}else{
			showmessage(L('参数错误'),"?m=tmall_zhekou&c=sgw_jizi&a=init");
		}

	}
	private function get_item_info($item_iid){
		$req = new ItemGetRequest;
		$req->setFields("detail_url,num_iid,title,pic_url,is_xinpin,price");
		$req->setTrackIid($item_iid);
		$resp = $this->taobao_api->execute($req);
		$item_info = $resp->item;
		$info['pic_url'] = $item_info->pic_url;
		$info['detail_url'] = $item_info->detail_url;
		$info['is_xinpin'] = $item_info->is_xinpin;
		$info['num_iid'] = $item_info->num_iid;
		$info['price'] = $item_info->price;
		$info['title'] = $item_info->title;
		$req = new UmpPromotionGetRequest;
		$req->setItemId($resp->item->num_iid);
		$resp = $this->taobao_api->execute($req);
		$promotions_in_item = $resp->promotions->promotion_in_item->promotion_in_item;
		$promotions_in_shop = $resp->promotions->promotion_in_shop->promotion_in_shop;
		//商品折扣
		$info['item_promo_start_time'] = $promotions_in_item[0]->start_time;
		$info['item_promo_end_time'] = $promotions_in_item[0]->end_time;
		$info['item_promo_price'] = $promotions_in_item[0]->item_promo_price;
		$info['item_promo_name'] = $promotions_in_item[0]->name;
		//店家折扣
		//var_dump($resp);
		$info['shop_promo_name'] = $promotions_in_shop[0]->name;
		$info['shop_promo_detail_desc'] = $promotions_in_shop[0]->promotion_detail_desc;
		//var_dump($info);
		return $info;
	}
	public function get_tmall_zhekou_items($para){
		//天猫折扣精品库
		$keyword = urlencode($para['q']);
		$start = ($para['start']-1)*10;
		$req = new TmallItemsDiscountSearchRequest;
		$req->setQ($keyword);
		$req->setStart($start);
		$req->setSort($para['sort']);
		if(isset($para['brand'])&&$para['brand']!=''){
			$req->setBrand($para['brand']);
		}
		if($para['post_fee'] =='-1'){
			$req->setPostFee(-1);
		}
		if(isset($para['cat'])&&$para['cat']!=''){
			$req->setCat($para['cat']);
		}
		$req->setStartPrice($para['start_price']);
		$req->setEndPrice($para['end_price']);
		$resp = $this->taobao_api->execute($req);
		return $resp;
	}
	private function __get_m_url($item_info){
		$temp = explode('_',$item_info['tb_item_id']);
		$item_id = $temp[0];
		//var_dump($item_info);exit;
		$url = "http://a.m.tmall.com/i".$item_id.".htm";
		$item_info['m_url'] = $url;
		return $item_info;
	}
	private function __tmall_sort_by($sort=''){
		$sort_arr = array(
			's'=>'人气排序',
			'p'=>'价格从高到低',
			'pd'=>'价格从低到高',
			'pt'=>'按发布时间排序',
			);
		$str  = "<option value='#1' #3>#2</option>";
		foreach($sort_arr as $k=>$v){
			$temp = str_replace('#1',$k,$str);
			if($sort == $k){
				$temp = str_replace('#3','selected',$temp);
			}
			$rstr .= str_replace('#2',$v,$temp);
		}
		return $rstr;
	}
}